package com.dyz.leetcode._20_dynamicProgramming;

public class _5_longestPalindrome {
    public String longestPalindrome(String s) {
        if(s==null || s.length()==0){return s;}
        boolean[][] dp = new boolean[s.length()][s.length()];
        String res = s.charAt(0)+"";
        //初始化
        for (int i = 0; i < s.length(); i++) {
            dp[i][i] = true;
        }

        for (int j = 1; j < s.length(); j++) {
            for (int i = 0; i < j; i++) {
                boolean isCharE = s.charAt(i)==s.charAt(j);
                if(j-i==1){
                    dp[i][j] = isCharE;
                }else if(!isCharE){
                    dp[i][j] = isCharE;
                }else {
                    dp[i][j] = dp[i+1][j-1];
                }

                if(dp[i][j] ==true && j-i+1>res.length()){
                    res = s.substring(i, j+1);
                }
            }
        }
        return res;

    }
}
